package com.iamteer.day24;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.Properties;


class User{
	String username;
	String password;
	public User(String username, String password) {
		this.username = username;
		this.password = password;
	}
	@Override
	public String toString() {
		return "{" + this.username + "\t" + this.password + "}";
	}
	
	@Override
	public boolean equals(Object obj) {
		User user = (User) obj;
		return this.username.equals(user.username) && this.password.equals(user.password);
	}
	
	@Override
	public int hashCode() {
		return (this.username + this.password).hashCode();
	}
}

public class UserServer extends Thread{

	Socket socket;
	static HashSet<User> users = new HashSet<User>();
	private static File file = new File("users.properties");
	private static Properties properties = new Properties();
	private static final int LOGIN_SUCCESS = 0;
	private static final int FAIL_WRONG_USERNAME = 1;
	private static final int FAIL_WRONG_PASSWORD = 2;
	
	public UserServer(Socket socket) {
		this.socket = socket;
	}

	static{
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	@Override
	public void run() {
//		System.out.println("start..." + this.getName());
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
			while (true) {
				String line = reader.readLine();
//				System.out.println(line);
				if (line != null) {
					String[] info = line.split(" ");
					String option = info[0];
					String username = info[1];
					String password = info[2];
					if ("A".equalsIgnoreCase(option)) {
						if( login(new User(username, password)) == UserServer.LOGIN_SUCCESS){
							writer.write("登录成功！\r\n");
						}else if( login(new User(username, password)) == UserServer.FAIL_WRONG_USERNAME){
							writer.write("账户不存在！\r\n");
						}else if( login(new User(username, password)) == UserServer.FAIL_WRONG_PASSWORD){
							writer.write("密码错误！\r\n");
						}
						writer.flush();
					} else if ("B".equalsIgnoreCase(option)) {
						if( register(new User(username, password)) ){
							writer.write("注册成功！\r\n");
						}else {
							writer.write("账户已存在，请重新注册！\r\n");
						}
						writer.flush();
					}else if ("Q".equalsIgnoreCase(option)) {
						break;
					}
				}
			}
			socket.close();
//			System.out.println("close...");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static int login(User user) {
		int ret;
		try {
			properties.load(new FileReader(file));
//			//遍历 Properties
//			int i=0;
//			Set<Entry<Object, Object>> entries = properties.entrySet();
//			for (Entry<Object, Object> entry : entries) {
//				System.out.println("i: " + i + entry.getKey() + "\t" +entry.getValue());
//				i++;
//			}
		} catch (IOException e) {
			e.printStackTrace();
		}
//		System.out.println(user.username);
		if (properties.containsKey(user.username)) {
//			System.out.println(user.password);
			String password = properties.getProperty(user.username);
			if (user.password.equals(password)) {
				ret = UserServer.LOGIN_SUCCESS;
			}else {
				ret = UserServer.FAIL_WRONG_PASSWORD;
			}
		}else {
			ret = UserServer.FAIL_WRONG_USERNAME;
		}
		
		return ret;
	}
	
	public static boolean register(User user) {
		boolean successful = false;
		if (!properties.containsKey(user.username)) {
			properties.setProperty(user.username, user.password);
			try {
				properties.store(new FileWriter(file), user.username);
			} catch (IOException e) {
				e.printStackTrace();
			}
			successful = true;
		}
		return successful;
	}
	
	public static void main(String[] args) throws IOException {
		ServerSocket serverSocket = new ServerSocket(9090);
		while (true) {
			Socket socket = serverSocket.accept();
			new UserServer(socket).start();
		}
	}

}
